// 1.首先获得所有的加法按钮
		var plusbtns = document.getElementsByName("plus");
// 2.首先获得所有的减法按钮
		var minusbtns = document.getElementsByName("minus");
// 3.然后获得所有数量文本框
		var amounts = document.getElementsByName("amount");
	
    var noneSelectImgSrc = "";
    var selectImgSrc = "";
	var shopCar;

    allSelectImg.onclick = function(){
      this.src = this.src == selectImgSrc?
          noneSelectImgSrc:selectImgSrc;
           totalPrice();
	
			
      var allSelectImgSrc = this.src;
                      
      $(".ckimg").each(function(index,ele){
          ele.src = allSelectImgSrc;
           totalPrice();
      });
    };
   

    window.onload = function() {
       
        $.showPreloader();
        $("#shopCarDiv").hide();
        var loadedImageCount = 0;

       shopCar = JSON.parse(localStorage.getItem("shopCar"));
        if (!shopCar||shopCar.length==0) {
            emptyCar.style.display = "block";
            quanxuan.style.display = "none";
            $.hidePreloader();
        } else {
            shopCarCount.innerHTML = "购物车("+ shopCar.length+")";
            for (var i = 0; i < shopCar.length; i++) {
                var product = shopCar[i];
                var productDiv = document.createElement("div");
                productDiv.className = "product";

                var selectImg = document.createElement("img");
                selectImg.src = noneSelectImgSrc;
                selectImg.style.height = "20px";
                selectImg.className = "ckimg";
                
                selectImg.onclick = function(){              	
//                  this.src = this.src == selectImgSrc
//                      ?noneSelectImgSrc:selectImgSrc;
                    if(this.src == selectImgSrc){
                        this.src = noneSelectImgSrc;
                        totalPrice();
                    }else{
                        this.src = selectImgSrc;
                        totalPrice();
                    }
                };

                var productImage = document.createElement("img");
                productImage.src = product.pic;
                productImage.style.height = "100px";
                productImage.onload = function(){
                    loadedImageCount++;
                    if(loadedImageCount==shopCar.length){
                        $.hidePreloader();
                        $("#shopCarDiv").show(300);
                    }
                };

                var productInfoDiv = document.createElement("div");

                var titleDiv = document.createElement("div");
                titleDiv.className = "titleDiv";
                titleDiv.innerHTML = product.title;

                var priceAndCountDiv = document.createElement("div");
                priceAndCountDiv.className = "priceAndCountDiv";

                var priceDiv = document.createElement("div");
                priceDiv.innerHTML = "&yen; " + product.price;   
                priceDiv.style.color = "red";
                priceDiv.style.fontSize ="16px";
                priceDiv.className = "prisDiv";
                

                priceAndCountDiv.appendChild(priceDiv);

                productInfoDiv.appendChild(titleDiv);
                productInfoDiv.appendChild(priceAndCountDiv);
                
                var inputDiv01 = document.createElement("input");
                inputDiv01.type = "button";
                inputDiv01.name = "minus";
                inputDiv01.value = "-";
                productInfoDiv.appendChild(inputDiv01);
                inputDiv01.onclick = function(){
                	jian();
                };
               
                var inputDiv02 = document.createElement("input");
                inputDiv02.style.width = "30px";
                inputDiv02.type = "text";
                inputDiv02.name = "amount";
                inputDiv02.className="countTxt";
                inputDiv02.value = product.count;
                productInfoDiv.appendChild(inputDiv02);
                
                
                var inputDiv03 = document.createElement("input");
                inputDiv03.type = "button";
                inputDiv03.value = "+";
                inputDiv03.name = "plus";
                productInfoDiv.appendChild(inputDiv03);
                inputDiv03.onclick = function(){
                	jia();
                };
                

            function jia(){        
            // 3.遍历所有的加法按钮
			for(var i=0;i<plusbtns.length;i++){
  			// 4. 让当前的按钮记住序号
  				plusbtns[i].index = i;
  			// 5. 当单击某一个按钮时，执行如下函数
  				plusbtns[i].onclick = function(){
    		// 6.this就是被点击的按钮，以后j就是序号
    				var j = this.index;
    		// 7.数量文本框的值等于原来数量文本框的值变成数字后再加1
    				amounts[j].value = parseInt(amounts[j].value) + 1
    				totalPrice();;
  				}
			}
		}  	
		
		    var txts = document.getElementsByClassName("countTxt");
			
			function jian(){	
			for(var i=0;i<minusbtns.length;i++){
  			// 4. 让当前的按钮记住序号
  				minusbtns[i].index = i;
  			// 5. 当单击某一个按钮时，执行如下函数
  				minusbtns[i].onclick = function(){
    		// 6.this就是被点击的按钮，以后j就是序号
    				var j = this.index;
					if (parseInt(txts[j].value) == 1) {
                		return;
            		}
    		// 7.数量文本框的值等于原来数量文本框的值变成数字后再加1
    				amounts[j].value = parseInt(amounts[j].value) -1;
    				totalPrice();
  				}
			}
			}
		

                var deleteBtn = document.createElement("input");
                deleteBtn.value = "删除";
                deleteBtn.type = "button";
                deleteBtn.style.float = "right"
                productInfoDiv.appendChild(deleteBtn);
                deleteBtn.nid = product.nid;
                deleteBtn.onclick = function(){
                    deleteProductFromShopCart(this,this.nid);
                    totalPrice();
                };

                productDiv.appendChild(selectImg);
                productDiv.appendChild(productImage);
                productDiv.appendChild(productInfoDiv);

                shopCarDiv.appendChild(productDiv);

            }
        }
    }
    
    
    //算总价
	function totalPrice() {
    var q = document.getElementsByClassName("ckimg");
    var pris = document.getElementsByClassName("prisDiv");
    var txts = document.getElementsByClassName("countTxt");
    var totalCount=0;
    for(var i=0;i<q.length;i++){

        if(q[i].src==selectImgSrc){
        	
			 var zhongjia=(parseFloat(pris[i].innerHTML.substring(1)))*(parseInt(txts[i].value));
			 
           totalCount +=zhongjia;
        }
    }

	allPrice.innerHTML="";
	allPrice.style.display ="inline-block";

    var allPriceTxt=document.createElement("span");
    allPriceTxt.innerHTML="总计：￥";
    var allPriceNum=document.createElement("span");
    allPriceNum.innerHTML=totalCount.toFixed(2);

    allPrice.appendChild(allPriceTxt);
    allPrice.appendChild(allPriceNum);

	payNow.style.display="inline-block";
    payNow.onclick=function () {
        for(var i=0;i<q.length;i++){

            if(q[i].src==selectImgSrc){
              location = 'http://test.hfjava.com/shop/zfbpay?' +
            'price=0.01&' +
            'subject='+"Under Armour"+'&' +
            'body=Under Armour&myorderno='+
            (new Date()).getTime()+"02";
            }
        }
    }
}
	
	totalPrice();
	

    
		function deleteProductFromShopCart(deleteBtn,nid){

        		for(var i = 0 ;i<shopCar.length;i++){
            
            	if(shopCar[i].nid == nid){
                	$.confirm('您确定要移除该宝贝吗?', function () {
                    	shopCar.splice(i,1);
                   	 	console.log(i);
                   	 	$(deleteBtn).parents(".product").css("height",
                        $(deleteBtn).parents(".product").height());
                    	setTimeout(function(){
                        	$(deleteBtn).parents(".product").css("opacity",0);
                        	$(deleteBtn).parents(".product").css("height",0);
                   		},1);
                   	 	shopCarCount.innerHTML = "购物车("+ shopCar.length+")";
                   	 	  totalPrice();
                    	$.toast("移除成功！");
                    	if(shopCar.length==0){
                        	emptyCar.style.display = "block";
                        	quanxuan.style.display = "none";
                    	}
                    	
                    localStorage.setItem("shopCar",JSON.stringify(shopCar));
                	});
                	break;
            }
        }   			
}
	